package scct.chenlixian.dao;

import scct.chenlixian.model.USer;
import scct.chenlixian.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//封装了User表的数据库操作
public class UserDao {
    //name:用户名

    public USer login(String name, String pass)  {
       USer user = null;
//        获取数据库连接对象
        Connection conn =DBUtils.getConnection();
        String sql = "select * from sm_user where username=? and password=?";
//        预编译查询对象
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setString(2, pass);
//            查询
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                int id = rs.getInt("id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                int age = rs.getInt("age");
                String gender = rs.getString("gender");
                String mobile = rs.getString("mobile");
                user = new USer(id, username, password, gender, age, mobile);
            }
            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }
    public boolean updateUser(int id,  String password, String gender,
                              int age, String mobile) {
        int count = 0;
        Connection conn = DBUtils.getConnection();
        String update_sql = "update sm_user set mobile=? ,age=?,gender=?, password=? where id=?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(update_sql);
            pstmt.setString(5, mobile);
            pstmt.setInt(4, age);
            pstmt.setString(3, gender);
            pstmt.setString(2, password);
            pstmt.setInt(1, id);
            count = pstmt.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count > 0;
    }

    public List<USer> getAllUser() {

        Connection conn = DBUtils.getConnection();
        List<USer> users = new ArrayList<>();

        String sql = "select * from sm_user";

        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String gender = rs.getString("gender");
                int age = rs.getInt("age");
                String mobile = rs.getString("mobile");
                users.add(new USer(id, username, password, gender, age, mobile));

            }
            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return users;
    }
    //判断用户是否存在
    public  boolean isDupname( String name) {

        //查找到用户数量
        int count = 0;
        Connection conn= DBUtils.getConnection();
        String sql = "select* from sm_user where username=?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                count = 1;
            }
            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count>0;
    }
    public boolean addUser(String username,String password,String gender,
                           int age ,String mobile) {
        int result = 0;
        Connection conn = DBUtils.getConnection();
        String insert_sql = "insert into sm_user values (null,?,?,?,?,?)";
        try {
            PreparedStatement pstmt = conn.prepareStatement(insert_sql);
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            pstmt.setString(3, gender);
            pstmt.setInt(4, age);
            pstmt.setString(5, mobile);
            result = pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result == 1;
    }
    public USer getUserById(int id) {
       USer user = null;
        Connection conn = DBUtils.getConnection();
        String sql = "select * from sm_user where id=?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                String username = rs.getString("username");
                String password = rs.getString("password");
                String mobile = rs.getString("mobile");
                String gender = rs.getString("gender");
                int age = rs.getInt("age");
                user = new USer(id,username,password,gender,age,mobile);

            }
            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    }


